package com.aliware.tianchi.loadbalance;

import org.apache.dubbo.rpc.Invoker;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author XDD
 * @project service
 * @date 2021/9/4
 * @description Good Good Study,Day Day Up.
 */
public class RoundRobinLB<T> implements CustomLoadBalance<T>{

    private final static Map<Class<?>,Integer> MAP = new ConcurrentHashMap<>(10);

    @Override
    public int lb(List<Invoker<T>> list) {
        if(list==null || list.size()<1){
            return -1;
        }
        Integer index = MAP.get(list.get(0).getClass());
        if(index==null){
            index=-1;
        }
        index++;

        if(index>=list.size()){
            index=0;
        }

        MAP.put(list.get(0).getClass(), index);
        return index;
    }
}
